package org.itboys.mobile.service.mongo.weixin;

import org.itboys.commons.ErrorException;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayYuyue;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 小程序_楼盘预约_service
 */

@Service
public class SundayWeiXinYuyueService extends BaseMongoService<SundayYuyue> {

    @Autowired
    private SundayValicodedateService valicodedateService;

    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayYuyue> getEntityClass() {
        return SundayYuyue.class;
    }

    /**
     * 保存用户楼盘预约表单
     * @param productId
     * @param memberId
     * @param phone
     * @param yzm
     */
    public void saveYuyue(Long productId, Long memberId, String phone, String yzm){

        //检验验证码, 里面包含了对手机号和验证码的校验
        valicodedateService.checkValidateCode(phone,yzm);

        Map<String, Object> param = new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("phone",phone);
        param.put("productId",productId);
        List<SundayYuyue> yuyues = super.list(param);

        if (yuyues.isEmpty()) {
            //保存用户预约情况
            super.save(new SundayYuyue(productId, memberId, phone));
        }else {
            throw new ErrorException("该手机已经预约过该楼盘了!");
        }
    }


}
